<?php
import( 'de.ceus-media.framework.krypton.core.DefinitionView' );
import( 'classes.view.panel.EnvironmentSelector' );
import( 'classes.model.Topic' );
import( 'classes.model.Run' );
import( 'classes.logic.Topic' );
import( 'classes.logic.Project' );
import( 'classes.logic.ServerProject' );
/**
 *	Option Views.
 *	@extends		Core_View
 *	@uses			View_Panel_PlatformSelector
 *	@author			Christian WÃ¼rker <Christian.Wuerker@CeuS-Media.de>
 *	@since			06.08.2007
 *	@version		0.1
 */
/**
 *	Option Views.
 *	@extends		Core_View
 *	@uses			View_Panel_PlatformSelector
 *	@author			Christian WÃ¼rker <Christian.Wuerker@CeuS-Media.de>
 *	@since			06.08.2007
 *	@version		0.1
 */
class View_Management_Run extends Framework_Krypton_Core_DefinitionView
{
	private $nodeId;

	/**
	 *	Builds Car Views.
	 *	@access		public
	 *	@return		string
	 */
	public function buildContent()
	{
		$request	= Framework_Krypton_Core_Registry::getStatic( 'request' );
		$session	= Framework_Krypton_Core_Registry::getStatic( 'session' );
		$selector	= new View_Panel_EnvironmentSelector();
		$selector->setServerProjects( true );
		$ui['content'] = $selector->buildContent();
		
		$projectId	= $session->get( 'projectId' );
		$runId		= $request->get( 'runId' );
		
		if( $runId )
		{
			$ui['content']	.= $this->buildContentEdit( $projectId, $runId );
		}
		else
		{
			$ui['content']	.= $this->buildContentList( $projectId );
			$ui['content']	.= $this->buildContentAdd( $projectId );
		}
		return $ui['content'];
		return $this->loadTemplate( 'management.run.content', $ui );
	}

	private function buildContentAdd( $projectId )
	{
		$request	= Framework_Krypton_Core_Registry::getStatic( 'request' );
		$session	= Framework_Krypton_Core_Registry::getStatic( 'session' );
		$words		= $this->words['run']['addRun'];
		
		$serverProjects	= Logic_ServerProject::getServerProjectList();
		foreach( $serverProjects as $serverProject )
		{
			$server		= Logic_Server::getServerTitle( $serverProject['server_id'] );
			$project	= Logic_Project::getProjectTitle( $serverProject['project_id'] );
			$opt_server_project[$serverProject['server_project_id']]	= $server." -> ".$project; 
		}
		
		$sources	= array( 'opt_server_project'	=> $opt_server_project ); 
		
		//  --  UI COMPONENTS  --  //
		$ui	= $this->buildForm( 'management/run', 'addRun', 'run', 'addRun', array(), $sources );
		$button_add		= UI_HTML_Elements::Button( 'addRun', $words['button_add'], 'but add' );
		$ui['field_button']	= UI_HTML_Elements::Field( '', $button_add );

		$ui['form']		= UI_HTML_Elements::Form( 'addRun', "?link=".$request->get( 'link' )."&projectId=".$projectId );
		$ui['caption']	= UI_HTML_Elements::TableCaption( $words['caption'], 'add' );
		$ui['colgroup']	= UI_HTML_Elements::ColumnGroup( $words['colgroup'] );
		return $this->loadTemplate( 'management.run.add', $ui );
	}

	private function buildContentEdit( $projectId, $runId )
	{
		$request	= Framework_Krypton_Core_Registry::getStatic( 'request' );
		$session	= Framework_Krypton_Core_Registry::getStatic( 'session' );
		$words		= $this->words['run']['editRun'];
		
		$data			= Logic_Run::getRunData( $runId );

		$opt_server_project	= Logic_Project::getProjects();
		$opt_server_project['_selected']	= $data['project_id'];
		$sources	= array( 'opt_server_project'	=> $opt_server_project ); 

		//  --  UI COMPONENTS  --  //
		$ui	= $this->buildForm( 'management/run', 'editRun', 'run', 'editRun', $data, $sources );
		$button_cancel	= UI_HTML_Elements::LinkButton( '?link='.$request->get( 'link' ), $words['button_cancel'], 'but cancel' );
		$button_edit	= UI_HTML_Elements::Button( 'editRun', $words['button_edit'], 'but edit' );
		$ui['field_button_cancel']	= UI_HTML_Elements::Field( '', $button_cancel );
		$ui['field_button_edit']	= UI_HTML_Elements::Field( '', $button_edit );

		$ui['form']		= UI_HTML_Elements::Form( 'editRun', "?link=".$request->get( 'link' )."&projectId=".$projectId."&runId=".$runId );
		$ui['caption']	= UI_HTML_Elements::TableCaption( $words['caption'], 'edit' );
		$ui['colgroup']	= UI_HTML_Elements::ColumnGroup( $words['colgroup'] );
		return $this->loadTemplate( 'management.run.edit', $ui );
	}

	private function buildContentList( $projectId )
	{
		$request	= Framework_Krypton_Core_Registry::getStatic( 'request' );
		$words		= $this->words['run']['listRuns'];
		$link		= $request->get( 'link' );
		$runs	= Logic_Run::getRunList( $projectId );
		$list	= array();
		$ui		= $this->words['run']['listRuns'];
		foreach( $runs as $run )
		{
			$run['action']	= "";
			if( $run['status'] < 2 )
			{
				$data	= array(
					'link'	=> $link,
					'runId'	=> $run['run_id'],
					'key'	=> 'startRun',
					'label' => $words['button_start'],
					);
				$run['action']	= $this->loadTemplate( 'management.run.link', $data );
			}
			else if( $run['status'] >= 2 && $run['status'] < 5 )
			{
				$data	= array(
					'link'	=> $link,
					'runId'	=> $run['run_id'],
					'key'	=> 'closeRun',
					'label' => $words['button_close'],
					);
				$run['action']	= $this->loadTemplate( 'management.run.link', $data );
			}
			$run['link']		= $link;
			$serverProject		= Logic_ServerProject::getServerProjectData( $run['server_project_id'] );
			$run['project']		= Logic_Project::getProjectTitle( $serverProject['project_id'] );
			$run['server']		= Logic_Server::getServerTitle( $serverProject['server_id'] );
			$run['status']		= $this->words['run']['states'][$run['status']];
			$run['created']		= $run['created'] ? date( "d.m.Y H:i", $run['created'] ) : "-";
			$run['started']		= $run['started'] ? date( "d.m.Y H:i", $run['started'] ) : "-";
			$run['modified']	= $run['modified'] ? date( "d.m.Y H:i", $run['modified'] ) : "-";
			$run['finished']	= $run['finished'] ? date( "d.m.Y H:i", $run['finished'] ) : "-";
			$list[]	= $this->loadTemplate( 'management.run.item', $run );
		}
		$ui['list']	= implode( "\n", $list );
		return $this->loadTemplate( 'management.run.list', $ui );
	}
}
?>
